//
//  RegistVC.m
//  BasePorject_App
//
//  Created by Yutian Duan on 2019/5/27.
//  Copyright © 2019年 Wanwin. All rights reserved.
//

#import "RegistVC.h"
#import "YFBaseTextField.h"

#define TextFieldFont 15.0f

@interface RegistVC ()

@property (nonatomic, strong) YFBaseTextField *phoneField;
@property (nonatomic, strong) YFBaseTextField *codeField;
@property (nonatomic, strong) UIButton *codeButton;
@property (nonatomic, strong) YFBaseTextField *passwordField;
@property (nonatomic, strong) YFBaseTextField *againField;

@end

@implementation RegistVC

- (UIButton *)codeButton {
  if (!_codeButton) {
    _codeButton = [[UIButton alloc] initWithFrame:CGRectMake(_codeField.yt_right + 10, _codeField.yt_top + 4, _codeField.yt_width - 10, _codeField.yt_height-8)];
    _codeButton.titleLabel.font = [UIFont systemFontOfSize:TextFieldFont];
    [_codeButton setTitle:YTLocalizedString(@"获取验证码") forState:UIControlStateNormal];
    [_codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _codeButton.layer.masksToBounds = YES;
    _codeButton.layer.cornerRadius = 8;
    _codeButton.backgroundColor = HEXCOLOR(0xbb2b25);
    [_codeButton addTarget:self action:@selector(sendCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_codeButton];
  }
  return _codeButton;
}

- (void)viewDidLoad {
  [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  [self setDefaltNavBar];
  [self setStatusBarStyleBack];
}

- (void)createFields {
  [super createFields];
  self.navigationItem.title = YTLocalizedString(@"会员注册");
  [self createBlackBack];
}

- (void)createViews {
  [super createViews];
  
  UILabel *stepLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, NavBarHeight + 40, ScreenWidth, 30)];
  stepLabel.text = [NSString stringWithFormat:@"%@:",YTLocalizedString(@"经以下简单步骤即可登记成为恒堡会会员")];
  stepLabel.font = [UIFont systemFontOfSize:13.0f];
  stepLabel.textAlignment = NSTextAlignmentCenter;
  stepLabel.textColor = DEF_GRAY;
  [self.view addSubview:stepLabel];
  
  
  CGFloat labelW = 90;
  CGFloat fontOffset = 0;
  CGFloat leftOffset = 20;
  if (IS_IPHONE5) {
    labelW = 80;
    fontOffset = 2;
    leftOffset = 15;
  }
  
  
  ///!
  UILabel *phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(leftOffset, stepLabel.yt_bottom + 20, labelW, 35)];
  phoneLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"手机号码")];
  phoneLabel.textColor = [UIColor whiteColor];
  phoneLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  phoneLabel.textAlignment = NSTextAlignmentLeft;
  phoneLabel.textColor = [UIColor blackColor];
  [self.view addSubview:phoneLabel];
  
  
  _phoneField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(phoneLabel.yt_right, phoneLabel.yt_top, self.view.yt_width - phoneLabel.yt_right - leftOffset, phoneLabel.yt_height)];
  [_phoneField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_phoneField set_placeTextColor:DEF_GRAY];
  _phoneField.placeholder = YTLocalizedString(@"输入手机号码");
  _phoneField.layer.masksToBounds = YES;
  _phoneField.layer.cornerRadius = 8;
  _phoneField.backgroundColor = [UIColor whiteColor];
  _phoneField.leftViewMode = UITextFieldViewModeAlways;
  _phoneField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [self.view addSubview:_phoneField];

  ///!
  UILabel *codeLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.yt_left, phoneLabel.yt_bottom + 20, phoneLabel.yt_width, phoneLabel.yt_height)];
  codeLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"手机验证码")];
  codeLabel.textColor = [UIColor whiteColor];
  codeLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  codeLabel.textAlignment = NSTextAlignmentLeft;
  codeLabel.textColor = [UIColor blackColor];
  [self.view addSubview:codeLabel];

  _codeField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_phoneField.yt_left, codeLabel.yt_top, _phoneField.yt_width/2, _phoneField.yt_height)];
  [_codeField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_codeField set_placeTextColor:DEF_GRAY];
  _codeField.placeholder = YTLocalizedString(@"输入短讯验证码");
  _codeField.layer.masksToBounds = YES;
  _codeField.layer.cornerRadius = 8;
  _codeField.backgroundColor = [UIColor whiteColor];
  _codeField.leftViewMode = UITextFieldViewModeAlways;
  _codeField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  
  [self.view addSubview:_codeField];
  
  ///!
  UILabel *passwordLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.yt_left, self.codeButton.yt_bottom + 20, phoneLabel.yt_width, phoneLabel.yt_height)];
  passwordLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"登录密码")];
  passwordLabel.textColor = [UIColor whiteColor];
  passwordLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  passwordLabel.textAlignment = NSTextAlignmentLeft;
  passwordLabel.textColor = [UIColor blackColor];
  [self.view addSubview:passwordLabel];
  
  
  _passwordField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_phoneField.yt_left, passwordLabel.yt_top, _phoneField.yt_width, _phoneField.yt_height)];
  [_passwordField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_passwordField set_placeTextColor:DEF_GRAY];
  _passwordField.placeholder = YTLocalizedString(@"输入英数组成8位以上之登录密码");
  _passwordField.layer.masksToBounds = YES;
  _passwordField.layer.cornerRadius = 8;
  _passwordField.backgroundColor = [UIColor whiteColor];
  _passwordField.leftViewMode = UITextFieldViewModeAlways;
  _passwordField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [self.view addSubview:_passwordField];

  
  ///!
  UILabel *aginLabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneLabel.yt_left, passwordLabel.yt_bottom + 20, phoneLabel.yt_width, phoneLabel.yt_height)];
  aginLabel.text = [NSString stringWithFormat:@"%@：",YTLocalizedString(@"确认密码")];
  aginLabel.textColor = [UIColor whiteColor];
  aginLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:TextFieldFont - fontOffset];
  aginLabel.textAlignment = NSTextAlignmentLeft;
  aginLabel.textColor = [UIColor blackColor];
  [self.view addSubview:aginLabel];
  
  
  _againField = [[YFBaseTextField alloc] initWithFrame:CGRectMake(_phoneField.yt_left, aginLabel.yt_top, _phoneField.yt_width, _phoneField.yt_height)];
  [_againField setTextFont:[UIFont systemFontOfSize:TextFieldFont - fontOffset]];
  [_againField set_placeTextColor:DEF_GRAY];
  _againField.placeholder = YTLocalizedString(@"重复输入以确认登录密码");
  _againField.layer.masksToBounds = YES;
  _againField.layer.cornerRadius = 8;
  _againField.backgroundColor = [UIColor whiteColor];
  _againField.leftViewMode = UITextFieldViewModeAlways;
  _againField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, _phoneField.yt_height)];
  [self.view addSubview:_againField];
  
  
  CGFloat buttonH = 30;
  if (IS_IPHONE5) {
    buttonH = 28;
  }
  
  ///!
  UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, _againField.yt_bottom + 40, 5 * buttonH, buttonH)];
//  [backButton setImage:[UIImage imageNamed:@"Regist_Back"] forState:UIControlStateNormal];
  backButton.yt_right = self.view.yt_width/2 - 10;
  backButton.backgroundColor = DEF_GRAY;
  backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  [backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [backButton setTitle:YTLocalizedString(@"返回登入") forState:UIControlStateNormal];
  backButton.layer.masksToBounds = YES;
  backButton.layer.cornerRadius = 8;
  [backButton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:backButton];
  
  ///!
  UIButton *submitButton = [[UIButton alloc] initWithFrame:CGRectMake(backButton.yt_right + 20, backButton.yt_top, backButton.yt_width, backButton.yt_height)];
//  [submitButton setImage:[UIImage imageNamed:@"Forget_submit"] forState:UIControlStateNormal];
  submitButton.backgroundColor = DEF_RED;
  submitButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
  [submitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  [submitButton setTitle:YTLocalizedString(@"提交") forState:UIControlStateNormal];
  submitButton.layer.masksToBounds = YES;
  submitButton.layer.cornerRadius = 8;

  [submitButton addTarget:self action:@selector(submitClick) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:submitButton];
  
}

- (void)backClick {
  [self.view endEditing: YES];
  [self goBackVC];
}

///! 验证码
- (void)sendCode {
  
  
  if (_phoneField.text.length == 0) {
    [SVProgressHUD showErrorWithStatus:YTLocalizedString(@"请输入手机号码")];
    return;
  }
  
  if (self.codeButton.selected == YES) {
    return;
  }
  
  __weak typeof(self) weakSelf = self;
  [[ToolsServer sharedInstance] sendmassgeCodeWithPhone:_phoneField.text finished:^(BOOL finish) {
    if (finish) {
      [SVProgressHUD showSuccessWithStatus:YTLocalizedString(@"验证码发送成功")];
      [weakSelf countDownT];
    }
  } failed:^(NSError *error) {
    
  }];
  
}

///! 注册
- (void)submitClick {
  if ([self checkEnabel] == NO) {
    return;
  }
  
  __weak typeof(self) weakSelf = self;
  [[ToolsServer sharedInstance] registAppWithAccount:_phoneField.text code:_codeField.text password:_passwordField.text againword:_againField.text finished:^(BOOL finish) {
    if (finish) {
      [weakSelf goBackVC];
  
    }
    
  } failed:^(NSError *error) {
    
  }];

  
}


- (BOOL)checkEnabel {
  if (_phoneField.text.length == 0) {
    [SVProgressHUD showErrorWithStatus:YTLocalizedString(@"请输入手机号码")];
    return NO;
  } else if (_codeField.text.length == 0) {
    [SVProgressHUD showErrorWithStatus:YTLocalizedString(@"请输入手机验证码")];
    return NO;
  } else if (_passwordField.text.length == 0) {
    [SVProgressHUD showErrorWithStatus:YTLocalizedString(@"请输入密码")];
    return NO;
  } else if (_againField.text.length == 0) {
    [SVProgressHUD showErrorWithStatus:YTLocalizedString(@"请再次输入密码")];
    return NO;
  } else if (![_passwordField.text isEqualToString:_againField.text]) {
    [SVProgressHUD showErrorWithStatus:YTLocalizedString(@"两次密码不一致，请重新输入")];
    return NO;
  }
  return YES;
}

- (void)countDownT {
  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  dispatch_block_t block = ^{
    int i = 60;
    while (i > 0) {
      if (i != 60) sleep(1);
      //耗时的操作
      NSLog(@"倒计时60秒----%d",i);
      i--;
      dispatch_async(dispatch_get_main_queue(), ^{
        if (self.codeButton) {
          if (i > 0) {
            // HEXCOLOR(0X888888)
            [self.codeButton setTitleColor:DEF_GRAY forState:UIControlStateNormal];
            [self.codeButton setTitle:[NSString stringWithFormat:@"%dS", i] forState:UIControlStateNormal];
          
            if (self.codeButton.selected == NO) {
              self.codeButton.selected = YES;
            }

          } else {
            [self.codeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            self.codeButton.selected = NO;
            [self.codeButton setTitle:YTLocalizedString(@"获取验证码") forState:UIControlStateNormal];
          }
        }
      });
    }
  };
  dispatch_async(queue, block);
}


@end
